package com.ztesoft.book.common.utils;

import org.apache.http.HttpStatus;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description 通用数据返回对象
 * @Author YangFan
 * @Date 2021-06-15 10:05
 */
public class Result extends HashMap<String, Object> {


    /**
     * 方法功能描述:
     * < code：1-成功   其他-失败 >
     *
     * @return null                            【返回值 注释说明】
     * @author YangFan
     * @date 2021-06-18 18:26
     */
    public Result() {
        put("code", 1);
        put("msg", "success");
    }

    public static Result success() {
        return new Result();
    }

    public static Result success(String msg) {
        Result r = new Result();
        r.put("msg", msg);
        return r;
    }

    public static Result success(Object obj) {
        Result r = new Result();
        r.put("data", obj);
        return r;
    }

    public static Result success(String msg, Object obj) {
        Result r = new Result();
        r.put("msg", msg);
        r.put("data", obj);
        return r;
    }

    public static Result success(Map<String, Object> map) {
        Result r = new Result();
        r.putAll(map);
        return r;
    }

    public static Result successNoMap(Map<String, Object> map) {
        Result r = new Result();
        r.put("data", map);
        return r;
    }


    public static Result error() {
        return error(HttpStatus.SC_INTERNAL_SERVER_ERROR, "未知服务器异常，请联系管理员");
    }

    public static Result error(String msg) {
        return error(HttpStatus.SC_INTERNAL_SERVER_ERROR, msg);
    }

    public static Result error(int code, String msg) {
        Result r = new Result();
        r.put("code", code);
        r.put("msg", msg);
        return r;
    }

    public static Result error(int code, Object obj) {
        Result r = new Result();
        r.put("code", code);
        r.put("data", obj);
        r.put("msg", "返回失败");
        return r;
    }

    public static Result error(int code, String msg, Object obj) {
        Result r = new Result();
        r.put("code", code);
        r.put("msg", msg);
        r.put("data", obj);
        return r;
    }

    public static Result error(int code, Map<String, Object> map) {
        Result r = new Result();
        r.put("code", code);
        r.putAll(map);
        r.put("msg", "返回失败");
        return r;
    }

    public static Result errorNoMap(int code, Map<String, Object> map) {
        Result r = new Result();
        r.put("code", code);
        r.put("data", map);
        r.put("msg", "返回失败");
        return r;
    }


    @Override
    public Result put(String key, Object value) {
        super.put(key, value);
        return this;
    }
}
